package net.xinqushi.common;

import java.util.ArrayList;
import java.util.List;

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException, NoSuchFieldException, IllegalAccessException {
        A a = new A("xjie",25);
        a.list.add(123);// shallow copy 浅copy, 两个对象
        Object b = a.clone();
        System.out.println(b == a);
        System.out.println(a.getClass().getDeclaredField("list").get(b) == a.list);
        a.name = "xiongjie";
        System.out.println(b);// 基本类型值copy, 引用类型地址copy

        int[] v1 = {1};
        v1[0] = 2;
        System.out.println(v1.clone()[0]);//数组自带clone
    }
}

class A implements Cloneable{
    List<Integer> list = new ArrayList<>();
    Integer age;
    String name;
    A(String name,Integer age){
        this.name=name;this.age=age;
    }
    A(){};
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "A{" +
                "list=" + list +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
